/**
/**
 * JAVACC DEMO 1.0
 */

package com.apache.excp.core.impl.imp;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.apache.excp.ConfigConstant;
import com.apache.excp.RuturnConfig;
import com.apache.excp.RuturnPropertyParam;
import com.apache.excp.core.manager.plugins.Execlplugins;

/**
 * description:  实现execl的导入插件
 * @author Hou Dayu 创建时间：2016-7-28
 */
public class ImplExeclPluginsImpl implements Execlplugins {

	private String configName = "xml/ImportExcelToModel.xml";
	private SAXReader saxReader;
	private Document doc;
	private Element root;

	/**
	 * TODO 实现execl的导入插件.  
	 */
	public Object execute(Map<String, Object> params) {
		String key = String.valueOf(params.get("methodName"));
		if ("getModel".equals(key)) {
			init(params);
			String modelName = String.valueOf(params.get("modelName"));
			String flag = String.valueOf(params.get("flag"));
			return getModel(modelName, flag);
		}
		return null;
	}

	private void init(Map<String, Object> param) {
		String str = ConfigConstant.getClassLoaderPath();
		// InputStream in = ClassLoader.getSystemResourceAsStream(configName);
		//InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(configName);
		saxReader = new SAXReader();
		try {
			InputStream in = new FileInputStream(str + configName);
			// Reader reader = Resources.getResourceAsReader(configName);
			// doc = saxReader.read(new File(str + configName));
			//saxReader.read(in)支持　UTF-8
			doc = saxReader.read(in);
			saxReader.setValidation(true);

			// saxReader.setContentHandler(this);
			// XMLErrorHandler error = new XMLErrorHandler();
			// saxReader.setErrorHandler(error);
			//    
			// saxReader.setFeature("http://xml.org/sax/features/namespaces",true);
			// saxReader.setFeature("http://xml.org/sax/features/validation",true);
			// saxReader.setFeature("http://apache.org/xml/features/validation/schema",
			// true);
			// saxReader.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
			//saxReader.read(reader); 不支持 UTF-8

			// doc = saxReader.read(reader);
			root = doc.getRootElement();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("rawtypes")
	private Element getModelElement(String modelName) {
		List list = root.elements();
		Element model = null;
		Element returnModel = null;
		for (Iterator it = list.iterator(); it.hasNext();) {
			model = (Element) it.next();
			//			System.out.println(model.attributeValue("id"));
			if (model.attributeValue("id").equals(modelName)) {
				returnModel = model;
				break;
			}
		}
		if (returnModel == null) {
			System.out.println("　＞＞＞　modelName : " + modelName + " 在配制文件中不存在！");
		}
		return returnModel;
	}

	private RuturnConfig getModel(String modelName, String flag) {
		Element model = this.getModelElement(modelName);
		RuturnConfig result = new RuturnConfig();
		if (model != null) {
			result.setClassName(model.attributeValue(ConfigConstant.MODEL_CLASS));
			// result.setPropertyMap(result,model);
			this.setPropertyMap(result, model);
		}
		return result;
	}

	@SuppressWarnings("rawtypes")
	private void setPropertyMap(RuturnConfig result, Element model) {
		Map<String, RuturnPropertyParam> propertyMap = new HashMap<String, RuturnPropertyParam>();
		Map<String, RuturnPropertyParam> columnMap = new HashMap<String, RuturnPropertyParam>();
		List list = model.elements();
		Element property = null;

		for (Iterator it = list.iterator(); it.hasNext();) {
			property = (Element) it.next();
			if (property.getName().equals("property")) {
				RuturnPropertyParam modelProperty = new RuturnPropertyParam();
				modelProperty.setName(property.attributeValue(ConfigConstant.PROPERTY_NAME));
				modelProperty.setColumn(property.attributeValue(ConfigConstant.PROPERTY_CLOUMN));
				modelProperty.setExcelTitleName(property.attributeValue(ConfigConstant.PROPERTY_EXCEL_TITLE_NAME));
				modelProperty.setIsNull(property.attributeValue(ConfigConstant.PROPERTY_ISNULL));
				modelProperty.setColumnWidth(property.attributeValue(ConfigConstant.PROPERTY_COLUMN_WIDTH));
				modelProperty.setDataType(property.attributeValue(ConfigConstant.PROPERTY_DATA_TYPE));
				modelProperty.setMaxLength(property.attributeValue(ConfigConstant.PROPERTY_MAX_LENGTH));

				modelProperty.setFixity(property.attributeValue(ConfigConstant.PROPERTY_FIXITY));
				modelProperty.setCodeTableName(property.attributeValue(ConfigConstant.PROPERTY_CODE_TABLE_NAME));
				modelProperty.setDefaultValue(property.attributeValue(ConfigConstant.PROPERTY_DEFAULT));

				String excelTitle = ConfigConstant.configValidate(propertyMap, modelProperty.getExcelTitleName());
				propertyMap.put(excelTitle, modelProperty);

				columnMap.put(modelProperty.getColumn(), modelProperty);
			}
			if (property.getName().equals("flag")) {
				Map<String, String> flagMap = new HashMap<String, String>();
				flagMap.put(ConfigConstant.PROPERTY_NAME, property.attributeValue(ConfigConstant.PROPERTY_NAME));
				result.setFlagMap(flagMap);
			}
			if (property.getName().equals("message")) {
				Map<String, String> messageMap = new HashMap<String, String>();
				messageMap.put(ConfigConstant.PROPERTY_NAME, property.attributeValue(ConfigConstant.PROPERTY_NAME));
				messageMap.put(ConfigConstant.PROPERTY_EXCEL_TITLE_NAME,
						property.attributeValue(ConfigConstant.PROPERTY_EXCEL_TITLE_NAME));
				result.setMessageMap(messageMap);
			}
		}
		result.setPropertyMap(propertyMap);
		result.setColumnMap(columnMap);
	}
}
